package controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.TestUser;
import service.TestUserManager;

@Controller
public class TestUserController {

	private ApplicationContext context;
	private TestUserManager manager;
	
	public TestUserController() {
		context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		manager = (TestUserManager) context.getBean("testUserManagerImpl");
	}
	
	@RequestMapping(value="/api/login", method=RequestMethod.POST)
	@ResponseBody
	public String doLogin(String uname, String passwd) {
		return manager.loginUser(uname, passwd) ? "Success!" : "Failed!";
	}
	
	@RequestMapping(value="/api/register", method=RequestMethod.POST)
	public String doRegister(String uname, String passwd, Integer age) {
		TestUser u = new TestUser();
		u.setUname(uname);
		u.setPasswd(passwd);
		u.setAge(age);
		manager.addUser(u);
		return "redirect:../login.html";
	}
}
